// pages/post/post.js
const app = getApp()
const db = wx.cloud.database({});
let comment = ''
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    postid: '',
    list: '',
    list1:'',
    comment: '',
    gzstatus: 'gz1',
    guanzhu:[],

  },

  navigateBack:function(){
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var postid = app.globalData.postid
    console.log(postid)
    this.setData({
      postid: postid,

    })
    const cont = db.collection('eas-qzxx')
    cont.where({
      _id: postid
    }).get({
      success: res => {
        this.setData({
          list1: res.data
        })
        const comment = res.data.comment
      }
    })
    const cont1 = db.collection('eas-comment')
    cont1.where({
      postid: postid
    }).get({
      success: res => {
        this.setData({
          list2: res.data
        })
        console.log(res)
      }
    })
  },
  getComment: function (e) {
    comment = e.detail.value

  },
  submitComment: function () {
    var year = new Date().getFullYear() // 示例
    var month = new Date().getMonth() + 1
    var day = new Date().getDate()
    var hours = new Date().getHours()
    var minutes = new Date().getMinutes()
    var seconds = new Date().getSeconds()
    const time = time
    var postid = app.globalData.postid
    var username = app.globalData.username
    var data = {
      comment: comment,
      username: username,
      time: year + '.' + month + '.' + day + ' ' + hours + ':' + minutes + ':' + seconds,
      postid: postid
    }
    //校验账号
    if (comment.length < 1) {
      wx.showToast({
        title: '评论内容为空',
        icon: "none"
      })
      return
    }
    if (comment.length > 100) {
      wx.showToast({
        title: '评论超过字数上限',
        icon: "error"
      })
      return
    }
    db.collection('eas-comment').add({
      data: data,
      success: res => {
        wx.showToast({
          title: '发布成功',
        })
        let _this = this
        _this.onLoad()
      },
      fail: e => {
        wx.showToast({
          title: '发布失败',
        })
        console.log(e)
      }

    })
    db.collection('eas-qzxx').doc(postid).update({
      data: {
        comment: _.inc(1)
      }

    })



  },

  IntoAccuse: function () {
    wx.navigateTo({
      url: '../accuse/accuse',
    })
  },

  guanzhu: function () {
    var year = new Date().getFullYear() // 示例
    var month = new Date().getMonth() + 1
    var day = new Date().getDate()
    var hours = new Date().getHours()
    var minutes = new Date().getMinutes()
    var seconds = new Date().getSeconds()
    var postid = app.globalData.postid
    var userid =app.globalData.userid
    var time = year + '.' + month + '.' + day + ' ' + hours + ':' + minutes + ':' + seconds
    if (this.data.gzstatus == 'gz1') {
      this.setData({
        gzstatus: 'gz2'
      })
      const cont2 = db.collection('eas-user')
      cont2.doc(userid).update({
        data:{
          guanzhu: _.push(postid)
        }
      })
      const cont3 = db.collection('eas-qzxx')
      cont3.doc(postid).update({
        data:{
          guanzhu: _.inc(1)
        }
      })
      console.log('关注成功')
      wx.showToast({
        title: '关注成功',
        icon: 'success'
      })
    } else {
      this.setData({
        gzstatus: 'gz1'
      })
      const cont4 = db.collection('eas-user')
      cont4.doc(userid).update({
        data:{
          guanzhu: _.pull(postid)
        }
      })
      const cont5 = db.collection('eas-qzxx')
      cont5.doc(postid).update({
        data:{
          guanzhu: _.inc(-1)
        }
      })
      console.log('已取消关注')
      wx.showToast({
        title: '已取消关注',
        icon: 'none'
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var userid=app.globalData.userid
    var postid = app.globalData.postid
    const cont6=db.collection('eas-user')
    if(userid!=''&&userid!=null){
      cont6.where({
        _id:userid
      }).get({}).then(res=>{
        this.setData({
          guanzhu1:res.data[0].guanzhu
        })
        console.log('guanzhu='+this.data.guanzhu1)
        var guanzhu1=this.data.guanzhu1
        console.log(guanzhu1)
        var i=guanzhu1.length
        for (var j=0;j<i;j++){
          if(guanzhu1[j]==postid){
            this.setData({
              gzstatus:'gz2'
            })
          }
        }
      })


    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})